<?php
//strlen,mb_strlen求字符串长度
$s="123计算机";
echo strlen($s),"\n";
echo mb_strlen($s),"\n";
//strcmp用于比较两个字符串的大小
var_dump(strcmp("Hello","Hi"));
var_dump(strcmp("Hi","hi"));
var_dump(strcmp("Hi~","Hi"));
var_dump(strcasecmp("Hi","hi"));

//trim,ltrim,rtrim删除字符串两侧，左侧，右侧的空白
$s="  \t \t How are you?  \t  ";
echo "[$s]\n";
echo "[",trim($s),"]\n";
echo "[",ltrim($s),"]\n";
echo "[",rtrim($s),"]\n";
echo "[",trim(",.;'Hello__","_.,';"),"]\n";
//strpos函数返回在字符串haystack中needle首次出现从0开始的位置
$s="How old are you?";
echo strpos($s,"o"),"\n";
echo strrpos($s,"o"),"\n";
$pos=0;
echo $pos=strpos($s,"o",$pos),"\n";
echo $pos=strpos($s,"o",++$pos),"\n";
echo $pos=strpos($s,"o",++$pos),"\n";
//bin2hex将字符串的每个字节转换为两个16进制位
$a='计算机123';
echo bin2hex($a),"\n";
echo hex2bin('e8aea1e7ae97e69cba313233'),"\n";
//chunk_split把字符串用指定的字符分隔开来
echo chunk_split("123456",2,":"),"/n";
echo chunk_split("e8aea1e7ae97e69cba313233",2," "),"/n";
echo chunk_split("e8aea1e7ae97e69cba313233",2,"_"),"/n";
//str_replace字符串替换
$s="How old are you?";
echo str_replace('o','**',$s),"\n";
//从字符串中，不管找到o还是r,统统替换成*
echo str_replace(['o','r'],'*',$s),"\n";
//字符串中，找到o替换成O，找到r替换成R
echo str_replace(['o','r'],['O','R'],$s),"/n";
//两个字符串，都按上面的规则处理
print_r(str_replace(['o','r'],['O','R'],[$s,'Hero']));
echo str_replace(' ','',$s),"\n";
//str_repeat多次重复字符串
echo str_repeat('Ok',3),"\n";
echo '[',str_repeat('Ok',0).']',"\n";
//join将数组元素拼接成字符串
echo join(['Hi','zhangsan']),"\n";
echo join(',',[123,456,789]),"\n";
//strtolower,strtoupper转换成小写，大写
echo strtolower("How are you.\n");
echo strtoupper("I'm fine, Thank you.\n");
echo strtoupper("e8_ae_a1_e7_ae_97\n");
//printf显示格式化字符串
$a=32.856;
printf("[%f] [%1$.1f] [%1$6.1f] [%1$-6.1f]\n",$a);
$s="Hello World";
printf("[%s] [%1$.5s] [%1$8.5s] [%1$'-8.5s]\n",$s);
$s="汉字";
printf("[%s] [%1$.3s] [%1$6.3s] [%1$'-8.5s]\n",$s);
//substr取子串
echo substr("Hello",0,3),"\n";
echo substr("Hello",2),"\n";
echo substr("Hello",0,-3),"\n";
echo substr("Hello",-4,-2),"\n";
//str_starts_with,str_ends_with以子串开始或结束
var_dump(str_starts_with("hello","ll"));
var_dump(str_starts_with("hello","he"));
var_dump(str_starts_with("hello","He"));
var_dump(str_ends_with("hello","llo"));


$a=['张三丰','里 斯','阿 kit','papi酱','Jackson'];
outFormat(35,$a);
function outFormat(int $width, ?array $a):void{
    if(is_null($a))return;
    foreach ($a as $name){
        $name=str_replace(' ','',$name);
        $hex=bin2hex($name);
        $hex=strtoupper($hex);
        $hex=chunk_split($hex,2,"_");
        $hex=substr($hex,0,-1);
        $lenName=strlen(iconv('utf-8','gbk',$name));
        $lenHex=strlen($hex);
        $lenSpace=$width-$lenName-$lenHex;
        if($lenSpace<0)$lenSpace=0;
        echo $name, str_repeat(' ',$lenSpace),$hex,"\n";
    }
}
